/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPnlSegmentsAutoSelection.java
 *
 * Created on 01/07/2010, 18:38:35
 */
package systole.view.tabs.segments;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import org.jdesktop.application.Action;
import org.jfree.chart.ChartPanel;
import systole.domain.signals.Segment;
import systole.processor.Qualifier;
import systole.processor.correlation.CorrelationKind;
import systole.processor.correlation.CorrelationResult;
import systole.propertiesController.SettingsProperties;
import systole.view.charts.XYChart;
import systole.view.tabs.JPnlSegments;
import systole.view.tabs.controllers.AnalysisController;
import systole.view.utils.ErrorDialog;

/**
 *
 * @author user
 */
public class JPnlAutoSelection extends javax.swing.JPanel implements Observer {

    /**
     *
     */
    private static final long serialVersionUID = -9001314568463374787L;
    private XYChart plot = null;
    private Segment selected = null;
    private JPnlItemAutoSelection selectedToDelete = null;
    private AnalysisController controller;
    private Vector<JPnlItemAutoSelection> items;
    private int x = 0;
    private int y = 0;
    private JPnlSegments parent;
    private boolean empty = false;

    /** Creates new form JPnlSegmentsAutoSelection 
     * @param controller 
     * @param parent 
     */
    public JPnlAutoSelection(AnalysisController controller, JPnlSegments parent) {
        super();
        initComponents();
        this.controller = controller;
        this.parent = parent;
        this.btnGroup.add(this.jRadioBtnAdd);
        this.btnGroup.add(this.jRadioBtnSet);
        this.jRadioBtnSet.setSelected(true);
        this.items = new Vector<JPnlItemAutoSelection>();
        this.jScrollPaneResult.getHorizontalScrollBar().setUnitIncrement(20);
        ComboBoxModel cbModel = new DefaultComboBoxModel(CorrelationKind.values());
        this.jCmbKind.setModel(cbModel);
        this.initCorrelation();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGroup = new javax.swing.ButtonGroup();
        jPnlCenter = new javax.swing.JPanel();
        jPnlTitle = new javax.swing.JPanel();
        jLblTitle = new javax.swing.JLabel();
        jPnlPlotSelected = new javax.swing.JPanel();
        jPnlSegment = new javax.swing.JPanel();
        jLblSegment = new javax.swing.JLabel();
        jPnlPlot = new javax.swing.JPanel();
        jPnlPlotRigth = new javax.swing.JPanel();
        jPnlPlotLeft = new javax.swing.JPanel();
        jPnlResult = new javax.swing.JPanel();
        jPnlResultTop = new javax.swing.JPanel();
        jPnlResultRigth = new javax.swing.JPanel();
        jPnlResultLeft = new javax.swing.JPanel();
        jScrollPaneResult = new javax.swing.JScrollPane();
        jPnlResultCenter = new javax.swing.JPanel();
        jPnlSummary = new javax.swing.JPanel();
        jPnlCorrSettings = new javax.swing.JPanel();
        jLblAmount = new javax.swing.JLabel();
        jLblKind = new javax.swing.JLabel();
        jBtnExecute = new javax.swing.JButton();
        jCmbKind = new javax.swing.JComboBox();
        jSpinnerAmount = new javax.swing.JSpinner();
        jLblCorrelation = new javax.swing.JLabel();
        jSpnCorrelation = new javax.swing.JSpinner();
        jPnlCorrResult = new javax.swing.JPanel();
        jRadioBtnSet = new javax.swing.JRadioButton();
        jRadioBtnAdd = new javax.swing.JRadioButton();
        jBtnSelect = new javax.swing.JButton();
        jLblAmountSelected = new javax.swing.JLabel();
        jLblAmountSelectedValue = new javax.swing.JLabel();
        jPnlTop = new javax.swing.JPanel();
        jPnlBottom = new javax.swing.JPanel();
        jPnlRigth = new javax.swing.JPanel();
        jPnlLeft = new javax.swing.JPanel();

        setName("Form"); // NOI18N
        setLayout(new java.awt.BorderLayout());

        jPnlCenter.setName("jPnlCenter"); // NOI18N
        jPnlCenter.setPreferredSize(new java.awt.Dimension(500, 495));
        jPnlCenter.setLayout(new java.awt.BorderLayout());

        jPnlTitle.setMaximumSize(new java.awt.Dimension(32767, 35));
        jPnlTitle.setMinimumSize(new java.awt.Dimension(0, 25));
        jPnlTitle.setName("jPnlTitle"); // NOI18N
        jPnlTitle.setPreferredSize(new java.awt.Dimension(669, 25));

        jLblTitle.setFont(jLblTitle.getFont().deriveFont(jLblTitle.getFont().getStyle() | java.awt.Font.BOLD, jLblTitle.getFont().getSize()+3));
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getResourceMap(JPnlAutoSelection.class);
        jLblTitle.setText(resourceMap.getString("jLblTitle.text")); // NOI18N
        jLblTitle.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLblTitle.setName("jLblTitle"); // NOI18N

        javax.swing.GroupLayout jPnlTitleLayout = new javax.swing.GroupLayout(jPnlTitle);
        jPnlTitle.setLayout(jPnlTitleLayout);
        jPnlTitleLayout.setHorizontalGroup(
            jPnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 590, Short.MAX_VALUE)
            .addGroup(jPnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPnlTitleLayout.createSequentialGroup()
                    .addGap(0, 208, Short.MAX_VALUE)
                    .addComponent(jLblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 209, Short.MAX_VALUE)))
        );
        jPnlTitleLayout.setVerticalGroup(
            jPnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 25, Short.MAX_VALUE)
            .addGroup(jPnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPnlTitleLayout.createSequentialGroup()
                    .addGap(0, 4, Short.MAX_VALUE)
                    .addComponent(jLblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 4, Short.MAX_VALUE)))
        );

        jPnlCenter.add(jPnlTitle, java.awt.BorderLayout.PAGE_START);

        jPnlPlotSelected.setName("jPnlPlotSelected"); // NOI18N
        jPnlPlotSelected.setPreferredSize(new java.awt.Dimension(300, 116));
        jPnlPlotSelected.setLayout(new java.awt.BorderLayout());

        jPnlSegment.setName("jPnlSegment"); // NOI18N
        jPnlSegment.setPreferredSize(new java.awt.Dimension(342, 25));

        jLblSegment.setFont(jLblSegment.getFont().deriveFont(jLblSegment.getFont().getStyle() | java.awt.Font.BOLD, jLblSegment.getFont().getSize()+2));
        jLblSegment.setText(resourceMap.getString("jLblSegment.text")); // NOI18N
        jLblSegment.setName("jLblSegment"); // NOI18N

        javax.swing.GroupLayout jPnlSegmentLayout = new javax.swing.GroupLayout(jPnlSegment);
        jPnlSegment.setLayout(jPnlSegmentLayout);
        jPnlSegmentLayout.setHorizontalGroup(
            jPnlSegmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 380, Short.MAX_VALUE)
            .addGroup(jPnlSegmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPnlSegmentLayout.createSequentialGroup()
                    .addGap(0, 124, Short.MAX_VALUE)
                    .addComponent(jLblSegment)
                    .addGap(0, 125, Short.MAX_VALUE)))
        );
        jPnlSegmentLayout.setVerticalGroup(
            jPnlSegmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 25, Short.MAX_VALUE)
            .addGroup(jPnlSegmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPnlSegmentLayout.createSequentialGroup()
                    .addGap(0, 5, Short.MAX_VALUE)
                    .addComponent(jLblSegment)
                    .addGap(0, 4, Short.MAX_VALUE)))
        );

        jPnlPlotSelected.add(jPnlSegment, java.awt.BorderLayout.PAGE_START);

        jPnlPlot.setName("jPnlPlot"); // NOI18N
        jPnlPlot.setLayout(new java.awt.BorderLayout());
        jPnlPlotSelected.add(jPnlPlot, java.awt.BorderLayout.CENTER);

        jPnlPlotRigth.setName("jPnlPlotRigth"); // NOI18N
        jPnlPlotRigth.setPreferredSize(new java.awt.Dimension(10, 91));

        javax.swing.GroupLayout jPnlPlotRigthLayout = new javax.swing.GroupLayout(jPnlPlotRigth);
        jPnlPlotRigth.setLayout(jPnlPlotRigthLayout);
        jPnlPlotRigthLayout.setHorizontalGroup(
            jPnlPlotRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPnlPlotRigthLayout.setVerticalGroup(
            jPnlPlotRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 274, Short.MAX_VALUE)
        );

        jPnlPlotSelected.add(jPnlPlotRigth, java.awt.BorderLayout.LINE_END);

        jPnlPlotLeft.setName("jPnlPlotLeft"); // NOI18N
        jPnlPlotLeft.setPreferredSize(new java.awt.Dimension(10, 91));

        javax.swing.GroupLayout jPnlPlotLeftLayout = new javax.swing.GroupLayout(jPnlPlotLeft);
        jPnlPlotLeft.setLayout(jPnlPlotLeftLayout);
        jPnlPlotLeftLayout.setHorizontalGroup(
            jPnlPlotLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPnlPlotLeftLayout.setVerticalGroup(
            jPnlPlotLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 274, Short.MAX_VALUE)
        );

        jPnlPlotSelected.add(jPnlPlotLeft, java.awt.BorderLayout.LINE_START);

        jPnlCenter.add(jPnlPlotSelected, java.awt.BorderLayout.CENTER);

        jPnlResult.setName("jPnlResult"); // NOI18N
        jPnlResult.setPreferredSize(new java.awt.Dimension(400, 200));
        jPnlResult.setLayout(new java.awt.BorderLayout());

        jPnlResultTop.setName("jPnlResultTop"); // NOI18N
        jPnlResultTop.setPreferredSize(new java.awt.Dimension(542, 6));

        javax.swing.GroupLayout jPnlResultTopLayout = new javax.swing.GroupLayout(jPnlResultTop);
        jPnlResultTop.setLayout(jPnlResultTopLayout);
        jPnlResultTopLayout.setHorizontalGroup(
            jPnlResultTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 590, Short.MAX_VALUE)
        );
        jPnlResultTopLayout.setVerticalGroup(
            jPnlResultTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 6, Short.MAX_VALUE)
        );

        jPnlResult.add(jPnlResultTop, java.awt.BorderLayout.NORTH);

        jPnlResultRigth.setName("jPnlResultRigth"); // NOI18N
        jPnlResultRigth.setPreferredSize(new java.awt.Dimension(8, 134));
        jPnlResult.add(jPnlResultRigth, java.awt.BorderLayout.LINE_END);

        jPnlResultLeft.setName("jPnlResultLeft"); // NOI18N
        jPnlResultLeft.setPreferredSize(new java.awt.Dimension(8, 134));

        javax.swing.GroupLayout jPnlResultLeftLayout = new javax.swing.GroupLayout(jPnlResultLeft);
        jPnlResultLeft.setLayout(jPnlResultLeftLayout);
        jPnlResultLeftLayout.setHorizontalGroup(
            jPnlResultLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 8, Short.MAX_VALUE)
        );
        jPnlResultLeftLayout.setVerticalGroup(
            jPnlResultLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 194, Short.MAX_VALUE)
        );

        jPnlResult.add(jPnlResultLeft, java.awt.BorderLayout.LINE_START);

        jScrollPaneResult.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPaneResult.setAutoscrolls(true);
        jScrollPaneResult.setName("jScrollPaneResult"); // NOI18N

        jPnlResultCenter.setName("jPnlResultCenter"); // NOI18N
        jPnlResultCenter.setLayout(new java.awt.GridBagLayout());
        jScrollPaneResult.setViewportView(jPnlResultCenter);

        jPnlResult.add(jScrollPaneResult, java.awt.BorderLayout.CENTER);

        jPnlCenter.add(jPnlResult, java.awt.BorderLayout.PAGE_END);

        jPnlSummary.setMaximumSize(new java.awt.Dimension(32767, 300));
        jPnlSummary.setMinimumSize(new java.awt.Dimension(0, 240));
        jPnlSummary.setName("jPnlSummary"); // NOI18N
        jPnlSummary.setPreferredSize(new java.awt.Dimension(210, 270));
        jPnlSummary.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPnlCorrSettings.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPnlCorrSettings.border.title"))); // NOI18N
        jPnlCorrSettings.setName("jPnlCorrSettings"); // NOI18N

        jLblAmount.setText(resourceMap.getString("jLblAmount.text")); // NOI18N
        jLblAmount.setName("jLblAmount"); // NOI18N

        jLblKind.setText(resourceMap.getString("jLblKind.text")); // NOI18N
        jLblKind.setName("jLblKind"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getActionMap(JPnlAutoSelection.class, this);
        jBtnExecute.setAction(actionMap.get("execute")); // NOI18N
        jBtnExecute.setFont(jBtnExecute.getFont().deriveFont(jBtnExecute.getFont().getSize()-1f));
        jBtnExecute.setIcon(resourceMap.getIcon("jBtnExecute.icon")); // NOI18N
        jBtnExecute.setText(resourceMap.getString("jBtnExecute.text")); // NOI18N
        jBtnExecute.setToolTipText(resourceMap.getString("jBtnExecute.toolTipText")); // NOI18N
        jBtnExecute.setName("jBtnExecute"); // NOI18N

        jCmbKind.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Segmento", "Resultado" }));
        jCmbKind.setToolTipText(resourceMap.getString("jCmbKind.toolTipText")); // NOI18N
        jCmbKind.setName("jCmbKind"); // NOI18N

        jSpinnerAmount.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(8), Integer.valueOf(1), null, Integer.valueOf(1)));
        jSpinnerAmount.setToolTipText(resourceMap.getString("jSpnAmount.toolTipText")); // NOI18N
        jSpinnerAmount.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jSpinnerAmount.setName("jSpnAmount"); // NOI18N

        jLblCorrelation.setText(resourceMap.getString("jLblCorrelation.text")); // NOI18N
        jLblCorrelation.setName("jLblCorrelation"); // NOI18N

        jSpnCorrelation.setModel(new javax.swing.SpinnerNumberModel(95, 1, 100, 1));
        jSpnCorrelation.setToolTipText(resourceMap.getString("jSpnCorrelation.toolTipText")); // NOI18N
        jSpnCorrelation.setName("jSpnCorrelation"); // NOI18N

        javax.swing.GroupLayout jPnlCorrSettingsLayout = new javax.swing.GroupLayout(jPnlCorrSettings);
        jPnlCorrSettings.setLayout(jPnlCorrSettingsLayout);
        jPnlCorrSettingsLayout.setHorizontalGroup(
            jPnlCorrSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlCorrSettingsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlCorrSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlCorrSettingsLayout.createSequentialGroup()
                        .addGroup(jPnlCorrSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLblCorrelation)
                            .addComponent(jLblAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                        .addGap(45, 45, 45)
                        .addGroup(jPnlCorrSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jSpinnerAmount)
                            .addComponent(jSpnCorrelation, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlCorrSettingsLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jBtnExecute, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPnlCorrSettingsLayout.createSequentialGroup()
                        .addComponent(jLblKind)
                        .addGap(18, 18, 18)
                        .addComponent(jCmbKind, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPnlCorrSettingsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jSpinnerAmount, jSpnCorrelation});

        jPnlCorrSettingsLayout.setVerticalGroup(
            jPnlCorrSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlCorrSettingsLayout.createSequentialGroup()
                .addGroup(jPnlCorrSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jCmbKind, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLblKind, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPnlCorrSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jSpnCorrelation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLblCorrelation))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPnlCorrSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER, false)
                    .addComponent(jSpinnerAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLblAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBtnExecute)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPnlCorrSettingsLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jSpinnerAmount, jSpnCorrelation});

        jLblAmount.getAccessibleContext().setAccessibleName(resourceMap.getString("jLblAmount.AccessibleContext.accessibleName")); // NOI18N

        jPnlSummary.add(jPnlCorrSettings, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 210, -1));

        jPnlCorrResult.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPnlCorrResult.border.title"))); // NOI18N
        jPnlCorrResult.setName("jPnlCorrResult"); // NOI18N

        jRadioBtnSet.setText(resourceMap.getString("jRadioBtnSet.text")); // NOI18N
        jRadioBtnSet.setToolTipText(resourceMap.getString("jRadioBtnSet.toolTipText")); // NOI18N
        jRadioBtnSet.setName("jRadioBtnSet"); // NOI18N

        jRadioBtnAdd.setText(resourceMap.getString("jRadioBtnAdd.text")); // NOI18N
        jRadioBtnAdd.setToolTipText(resourceMap.getString("jRadioBtnAdd.toolTipText")); // NOI18N
        jRadioBtnAdd.setName("jRadioBtnAdd"); // NOI18N

        jBtnSelect.setAction(actionMap.get("select")); // NOI18N
        jBtnSelect.setIcon(resourceMap.getIcon("jBtnSelect.icon")); // NOI18N
        jBtnSelect.setText(resourceMap.getString("jBtnSelect.text")); // NOI18N
        jBtnSelect.setName("jBtnSelect"); // NOI18N

        jLblAmountSelected.setText(resourceMap.getString("jLblAmountSelected.text")); // NOI18N
        jLblAmountSelected.setName("jLblAmountSelected"); // NOI18N

        jLblAmountSelectedValue.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLblAmountSelectedValue.setText(resourceMap.getString("jLblAmountSelectedValue.text")); // NOI18N
        jLblAmountSelectedValue.setName("jLblAmountSelectedValue"); // NOI18N

        javax.swing.GroupLayout jPnlCorrResultLayout = new javax.swing.GroupLayout(jPnlCorrResult);
        jPnlCorrResult.setLayout(jPnlCorrResultLayout);
        jPnlCorrResultLayout.setHorizontalGroup(
            jPnlCorrResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlCorrResultLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlCorrResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnlCorrResultLayout.createSequentialGroup()
                        .addGroup(jPnlCorrResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLblAmountSelected)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlCorrResultLayout.createSequentialGroup()
                                .addGroup(jPnlCorrResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jRadioBtnAdd)
                                    .addComponent(jRadioBtnSet))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                        .addComponent(jLblAmountSelectedValue))
                    .addComponent(jBtnSelect, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPnlCorrResultLayout.setVerticalGroup(
            jPnlCorrResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlCorrResultLayout.createSequentialGroup()
                .addGroup(jPnlCorrResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLblAmountSelected)
                    .addComponent(jLblAmountSelectedValue))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(jRadioBtnAdd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioBtnSet)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnSelect)
                .addGap(6, 6, 6))
        );

        jPnlSummary.add(jPnlCorrResult, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 157, -1, -1));

        jPnlCenter.add(jPnlSummary, java.awt.BorderLayout.LINE_END);

        add(jPnlCenter, java.awt.BorderLayout.CENTER);

        jPnlTop.setMaximumSize(new java.awt.Dimension(32767, 10));
        jPnlTop.setMinimumSize(new java.awt.Dimension(0, 10));
        jPnlTop.setName("jPnlTop"); // NOI18N
        jPnlTop.setPreferredSize(new java.awt.Dimension(689, 4));

        javax.swing.GroupLayout jPnlTopLayout = new javax.swing.GroupLayout(jPnlTop);
        jPnlTop.setLayout(jPnlTopLayout);
        jPnlTopLayout.setHorizontalGroup(
            jPnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 610, Short.MAX_VALUE)
        );
        jPnlTopLayout.setVerticalGroup(
            jPnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 4, Short.MAX_VALUE)
        );

        add(jPnlTop, java.awt.BorderLayout.PAGE_START);

        jPnlBottom.setMaximumSize(new java.awt.Dimension(32767, 10));
        jPnlBottom.setMinimumSize(new java.awt.Dimension(0, 10));
        jPnlBottom.setName("jPnlBottom"); // NOI18N
        jPnlBottom.setPreferredSize(new java.awt.Dimension(689, 8));

        javax.swing.GroupLayout jPnlBottomLayout = new javax.swing.GroupLayout(jPnlBottom);
        jPnlBottom.setLayout(jPnlBottomLayout);
        jPnlBottomLayout.setHorizontalGroup(
            jPnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 610, Short.MAX_VALUE)
        );
        jPnlBottomLayout.setVerticalGroup(
            jPnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 8, Short.MAX_VALUE)
        );

        add(jPnlBottom, java.awt.BorderLayout.PAGE_END);

        jPnlRigth.setMaximumSize(new java.awt.Dimension(10, 32767));
        jPnlRigth.setMinimumSize(new java.awt.Dimension(10, 0));
        jPnlRigth.setName("jPnlRigth"); // NOI18N
        jPnlRigth.setPreferredSize(new java.awt.Dimension(10, 202));

        javax.swing.GroupLayout jPnlRigthLayout = new javax.swing.GroupLayout(jPnlRigth);
        jPnlRigth.setLayout(jPnlRigthLayout);
        jPnlRigthLayout.setHorizontalGroup(
            jPnlRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPnlRigthLayout.setVerticalGroup(
            jPnlRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 524, Short.MAX_VALUE)
        );

        add(jPnlRigth, java.awt.BorderLayout.LINE_END);

        jPnlLeft.setMaximumSize(new java.awt.Dimension(10, 32767));
        jPnlLeft.setMinimumSize(new java.awt.Dimension(10, 0));
        jPnlLeft.setName("jPnlLeft"); // NOI18N
        jPnlLeft.setPreferredSize(new java.awt.Dimension(10, 202));

        javax.swing.GroupLayout jPnlLeftLayout = new javax.swing.GroupLayout(jPnlLeft);
        jPnlLeft.setLayout(jPnlLeftLayout);
        jPnlLeftLayout.setHorizontalGroup(
            jPnlLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPnlLeftLayout.setVerticalGroup(
            jPnlLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 524, Short.MAX_VALUE)
        );

        add(jPnlLeft, java.awt.BorderLayout.LINE_START);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGroup;
    private javax.swing.JButton jBtnExecute;
    private javax.swing.JButton jBtnSelect;
    private javax.swing.JComboBox jCmbKind;
    private javax.swing.JLabel jLblAmount;
    private javax.swing.JLabel jLblAmountSelected;
    private javax.swing.JLabel jLblAmountSelectedValue;
    private javax.swing.JLabel jLblCorrelation;
    private javax.swing.JLabel jLblKind;
    private javax.swing.JLabel jLblSegment;
    private javax.swing.JLabel jLblTitle;
    private javax.swing.JPanel jPnlBottom;
    private javax.swing.JPanel jPnlCenter;
    private javax.swing.JPanel jPnlCorrResult;
    private javax.swing.JPanel jPnlCorrSettings;
    private javax.swing.JPanel jPnlLeft;
    private javax.swing.JPanel jPnlPlot;
    private javax.swing.JPanel jPnlPlotLeft;
    private javax.swing.JPanel jPnlPlotRigth;
    private javax.swing.JPanel jPnlPlotSelected;
    private javax.swing.JPanel jPnlResult;
    private javax.swing.JPanel jPnlResultCenter;
    private javax.swing.JPanel jPnlResultLeft;
    private javax.swing.JPanel jPnlResultRigth;
    private javax.swing.JPanel jPnlResultTop;
    private javax.swing.JPanel jPnlRigth;
    private javax.swing.JPanel jPnlSegment;
    private javax.swing.JPanel jPnlSummary;
    private javax.swing.JPanel jPnlTitle;
    private javax.swing.JPanel jPnlTop;
    private javax.swing.JRadioButton jRadioBtnAdd;
    private javax.swing.JRadioButton jRadioBtnSet;
    private javax.swing.JScrollPane jScrollPaneResult;
    private javax.swing.JSpinner jSpinnerAmount;
    private javax.swing.JSpinner jSpnCorrelation;
    // End of variables declaration//GEN-END:variables

    private void initCorrelation() {
        SettingsProperties correlationProperties = new SettingsProperties();
        this.jCmbKind.setSelectedItem(correlationProperties.loadCorrelationKind());
        this.jSpinnerAmount.setValue(correlationProperties.loadMaxAmountOfSegments());
        this.jSpnCorrelation.setValue(correlationProperties.loadCorrelationValue());
    }

    /**
     *
     */
    private void clearResults() {
        this.jPnlResultCenter.removeAll();
        this.items.removeAllElements();
        this.jPnlResultCenter.setLayout(new GridBagLayout());
        this.jPnlResultCenter.setBackground(this.jPnlResultCenter.getBackground());
        this.jPnlResultCenter.repaint();
        this.jPnlResultCenter.revalidate();
        this.jScrollPaneResult.revalidate();
    }

    /**
     *
     */
    private void clearSelectedSegment() {
        this.jPnlPlot.removeAll();
        this.jPnlPlot.revalidate();
        this.empty = true;
    }

    /**
     *
     */
    public void clearSelection() {
        this.clearSelectedSegment();
        this.clearResults();
    }

    /**
     *
     * @param segment
     */
    public void loadSegmentToCorrelation(Segment segment) {
        this.clearSelection();
        if (segment != null) {
            this.empty = false;
            this.selected = segment;
            this.plot = new XYChart(null);
            this.plot.setShowLegend(false);
            this.plot.setShowTitle(false);
            this.plot.addSeries("Selected segment", this.selected.toDoubleArray(), 1);
            ChartPanel pnl = this.plot.plot();
            pnl.setMouseZoomable(false);
            pnl.setPopupMenu(null);
            this.jPnlPlot.add(pnl, BorderLayout.CENTER);
            this.jPnlPlot.revalidate();
        }
    }

    /**
     *
     */
    protected void process() {
        this.clearResults();
        Integer value = (Integer) this.jSpnCorrelation.getValue();
        BigDecimal cor = new BigDecimal(value);

        if ((cor.doubleValue() > 100) || (cor.doubleValue() < 1)) {
            ErrorDialog.showError(this, "El grado de correlación debe estar entre 1 y 100");
        }

        cor = cor.divide(new BigDecimal(100));
        Integer maxAmount = (Integer) this.jSpinnerAmount.getValue();

        if ((maxAmount == null) || ((maxAmount.intValue() < 0))) {
            ErrorDialog.showError(this, "La cantidad Máxima de segmentos a seleccionar debe ser mayo a cero");
        }

        CorrelationKind kind = (CorrelationKind) this.jCmbKind.getSelectedItem();
        Qualifier qualifier = new Qualifier(selected, cor, maxAmount.intValue(), kind);
        this.loadResults(qualifier.qualify(this.controller.getAnalysis().getAllSegments()));
        this.empty = false;
    }

    /**
     *
     */
    private void loadResults(List<CorrelationResult> segments) {
        if (segments != null) {
            Iterator<CorrelationResult> it = segments.iterator();
            CorrelationResult currentSegment;
            Segment segment;
            XYChart chart;
            int currentPos;
            String name;
            ChartPanel pnl;
            if ((x == 0) || (y == 0)) {
                x = this.jPnlResultCenter.getWidth() / 4;
                y = this.jPnlResultCenter.getHeight() - 15;
            }
            Dimension dim = new Dimension(x, y);
            DecimalFormat format = new DecimalFormat("####.00");
            while (it.hasNext()) {
                currentSegment = it.next();
                //currentPos = this.controller.getAnalysis().getAllSegments().getSegments().indexOf(currentSegment);
                segment = this.controller.getAnalysis().getAllSegments().getSegments().elementAt(currentSegment.getSegmentNumber());
                name = "Segmento: " + currentSegment.getSegmentNumber();
                chart = new XYChart(name);
                chart.addSeries(name, segment.toDoubleArray(), 1);
                chart.setShowLegend(false);
                chart.setShowTitle(false);
                pnl = chart.plot();
                pnl.setMouseZoomable(false);
                pnl.setPopupMenu(null);
                BigDecimal correlation = currentSegment.getCorrelation().multiply(new BigDecimal(100));
                String hint = "Grado de correlación: " + format.format(correlation);
                JPnlItemAutoSelection img = new JPnlItemAutoSelection(currentSegment.getSegmentNumber(), pnl, this, hint);
                img.setPreferredSize(dim);
                img.revalidate();
                this.jPnlResultCenter.add(img);
                this.items.add(img);
            }

            this.jLblAmountSelectedValue.setText(String.valueOf(segments.size()));
            this.jScrollPaneResult.getHorizontalScrollBar().setBlockIncrement(x);
            this.jPnlResultCenter.revalidate();
        }

    }

    /**
     * execute autocorrelation
     */
    @Action
    public void execute() {
        this.process();
    }

    /**
     * @param pnlToRemove
     */
    public void setAsSelected(JPnlItemAutoSelection pnlToRemove) {
        if (this.selectedToDelete != null) {
            this.selectedToDelete.unclick();
        }
        this.selectedToDelete = pnlToRemove;
    }

    /**
     *
     * @param o
     * @param arg
     */
    public void update(Observable o, Object arg) {
        return;
    }

    /**
     * @param pnlToRemove
     */
    public void removeSegment(JPnlItemAutoSelection pnlToRemove) {
        this.jPnlResultCenter.remove(pnlToRemove);
        this.jPnlResultCenter.setLayout(new GridBagLayout());
        this.jPnlResultCenter.setBackground(this.jPnlResultCenter.getBackground());
        this.jPnlResultCenter.repaint();
        this.jPnlResultCenter.revalidate();
        this.jScrollPaneResult.revalidate();
        this.items.remove(pnlToRemove);
        this.jLblAmountSelectedValue.setText(String.valueOf(this.items.size()));
    }

    /**
     * Select the current list of segments
     */
    @Action
    public void select() {

        if ((this.items == null) || (this.items.isEmpty())) {
            return;
        }
        // si no quiere utilizar los anteriores vacio la lista
        if (this.jRadioBtnSet.isSelected()) {
            this.controller.clearSelection();
        }

        // busco la selección generada
        Iterator<JPnlItemAutoSelection> segments = this.items.iterator();
        JPnlItemAutoSelection pnl;
        while (segments.hasNext()) {
            pnl = segments.next();
            pnl.select();
            this.controller.getSelectionModel().addSegment(pnl.getSegmentIndex());
        }


    }
}
